package com.shiro.exception;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理类  (作用: 怎么去使用异常类 ,使用异常类要做什么事)
 */

@RestControllerAdvice
public class MyExceptionHandler {

    /** @ExceptionHandler  表示 异常拦截后 ,要执行什么功能 , Exception.class 代表可以处理所以异常
     *
     * @param e     可以获取到异常内部一些信息
     * @param request   可以获取 访问的路径
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Object handlerException(Exception e, HttpServletRequest request){
        return MyException.error().setMsg(e.getMessage()).setUrl(request.getRequestURI());
    }


    @ExceptionHandler(TokenExpiredException.class)
    public Object TokenExpiredException(TokenExpiredException e,HttpServletRequest request){
        return MyException.error().setMsg("令牌过期异常").setUrl(request.getRequestURI());
    }

    @ExceptionHandler(AlgorithmMismatchException.class)
    public Object AlgorithmMismatchException(AlgorithmMismatchException e,HttpServletRequest request){
        return MyException.error().setMsg("算法不匹配异常").setUrl(request.getRequestURI());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public Object IllegalArgumentException(IllegalArgumentException e,HttpServletRequest request){
        return MyException.error().setMsg("token不能为空").setUrl(request.getRequestURI());
    }


}
